package com.mlnx.push.websocket.server.session;

import com.alibaba.fastjson.JSON;
import com.mlnx.mptp.push.body.SerialType;
import com.mlnx.push.websocket.server.topic.Topic;
import com.mlnx.push.websocket.server.topic.UserTopicManager;

import org.shan.mq.session.core.NettySession;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lombok.Data;

/**
 * Created by amanda.shan on 2019/10/31.
 */
@Data
public class WebsocketPushNettySession extends NettySession {

    private List<Topic> topics; // 主题 用户消息分发

    private SerialType pushSerialType;

    public void setTopics(List<Topic> topics) {
        this.topics = topics;

        Set<String> topicFilters = new HashSet();

        if (topics != null) {
            for (int i = 0; i < topics.size(); i++) {
                Topic topic = topics.get(i);
                topicFilters.add(JSON.toJSONString(topic));
            }
        }
        setTopicFilters(topicFilters);
    }

    public void addTopics(List<Topic> topics) {
        for (Topic topic : topics) {
            if (!getTopics().contains(topic)) {
                this.topics.add(topic);
            }
        }

        setTopics(topics);
    }

    public void removeTopics(List<Topic> topics) {
        for (Topic topic : topics) {
            if (getTopics().contains(topic)) {
                this.topics.remove(topic);
            }
        }

        setTopics(topics);
    }

    @Override
    public String toString() {
        return "WebsocketPushNettySession{" +
                "topics=" + topics +
                ", pushSerialType=" + pushSerialType
                + super.toString() +
                '}';
    }

    public static void main(String[] args) {

        UserTopicManager manager = new UserTopicManager().lisBp(1).lisBt(1);
        List<Topic> topics = manager.getTopics();
        for (int i = 0; i < topics.size(); i++) {
            Topic topic = topics.get(i);
            System.out.println(JSON.toJSONString(topic));
        }

    }
}
